package com.ecy.quadratic.ui.fragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.ComicCollectAdapter;
import com.ecy.quadratic.beans.ComicDetail;
import com.ecy.quadratic.beans.support.ComicCollectEvent;
import com.ecy.quadratic.component.DaggerMineComponent;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.activity.ComicDetailActivity;
import com.ecy.quadratic.ui.contract.ComicCollectContract;
import com.ecy.quadratic.ui.presenter.ComicCollectPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;


/**
 * Created by Administrator on 2016/3/31.
 */
public class ComicCollectFragment extends BaseFragment implements ComicCollectContract.View {

    @Bind(R.id.comic_collect_listview)
    ListView mComicListView;
    @Bind(R.id.empty_iv)
    ImageView mEmptyIV;

    @Inject
    ComicCollectPresenter mBrowserPresenter;

    private ComicCollectAdapter mAdapter;
    private List<ComicDetail> mComicList;

    public ComicCollectFragment() {
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_comic_collect;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMineComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mBrowserPresenter != null) {
            mBrowserPresenter.attachView(this);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mBrowserPresenter != null)
            mBrowserPresenter.detachView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {
        mComicList = new ArrayList<ComicDetail>();
        mAdapter = new ComicCollectAdapter(getContext(), mComicList);
        mBrowserPresenter.getCollect();
    }

    @Override
    public void configViews() {
        mComicListView.setAdapter(mAdapter);
        mComicListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ComicDetailActivity.startActivity(getContext(), mAdapter.getItem(position));
            }
        });

    }

    @Override
    public void showCollect(List<ComicDetail> list) {
        if (list != null && list.size() > 0) {
            mEmptyIV.setVisibility(View.GONE);
        }
        mAdapter.updateData(list);
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void refreshCollectList(ComicCollectEvent event) {
        mBrowserPresenter.getCollect();
    }
}
